<?php
$year = date("Y");
echo '<form style="margin: 10px 0; border-bottom: 1px solid gray; padding-bottom: 2px;" action="home.php?akc=91" method="post" id="vystupy">';
$formID = "'vystupy'";
echo '<select name="tyzden" size="1" onchange="submitForm('.$formID.')">';
echo '<option value="0">Select...</option>';
if (isset($_POST['tyzden'])&&($_POST['tyzden']==1))
    echo '<option value="1" selected>Sick days '.$year.'</option>';
else
    echo '<option value="1">Sick days '.$year.'</option>';
if (isset($_POST['tyzden'])&&($_POST['tyzden']==2))
    echo '<option value="2" selected>LV days '.$year.'</option>';
else
    echo '<option value="2">LV days '.$year.'</option>';
if (isset($_POST['tyzden'])&&($_POST['tyzden']==3))
    echo '<option value="3" selected>Vacation list '.$year.'</option>';
else
    echo '<option value="3">Vacation list '.$year.'</option>';
echo '</select>';
echo '</form>';
$print = false;
if (isset($_POST['tyzden'])&&($_POST['tyzden']==1)) {
    $priznak = 'PN';
    $print = true;
} else if (isset($_POST['tyzden'])&&($_POST['tyzden']==2)) {
    $priznak = 'LV';
    $print = true;
} else if (isset($_POST['tyzden'])&&($_POST['tyzden']==3)) {
    $priznak = 'D';
    $print = true;
} else {
    echo 'Vyberte si tabuľku.';
    $print = false;
}

if ($print) {
    $months = array('January','February','March','April','May','June','July','August','September','Oktober','November','December');
    echo '<table style="border-collapse: collapse">';
    echo '<tr>';
    echo '<th style="width: 150px; background: #ddd; border: 1px solid black">Name</th>';
    for ($i=0;$i<12;$i++) {
        echo '<th style="width: 80px; background: #eee; border: 1px solid black">'.$months[$i].'</th>';
    }
    echo '<th style="width: 80px; background: #ddd; border: 1px solid black">Total</th>';
    echo '</tr>';
    $zam_s = "SELECT * FROM z WHERE spolid=46 ORDER BY priezvisko ASC";
    $zam_q = MySQL_Query ($zam_s);
    while ($zam = MySQL_Fetch_Array($zam_q)) {
        if ($zam['datum_odchodu']=='0000-00-00') {
            $odchod = date("U",mktime(0,0,0,12,31,2020));
        } else {
            $o = explode('-', $zam['datum_odchodu']);
            $odchod = date("U",mktime(0,0,0,$o[1],$o[2],$o[0]));
        }
        $p = explode('-', $zam['datum_prichodu']);
        $prichod = date("U",mktime(0,0,0,$p[1],$p[2],$p[0]));

        $zr = date("U",mktime(0,0,0,1,1,$year));
        $kr = date("U",mktime(0,0,0,12,31,$year));
        $total = 0;
        if (!((($prichod<$zr)&&($odchod<$zr))||(($prichod>$kr)&&($odchod>$kr)))) {
            echo '<tr id="'.$zam['id'].'">';
            echo '<td style="background: #eee; border: 1px solid gray" onmouseover="zvyrazni('.$zam['id'].',1)" onmouseout="zvyrazni('.$zam['id'].',0)">';
            echo $zam['meno'] . ' ' . $zam['priezvisko'];
            echo '</td>';
            for ($i=0;$i<12;$i++) {
                $pocet = 0;
                $d_s = "SELECT date FROM dochadzkazamfalk WHERE zid='".$zam['id']."' AND type='".$priznak."'";
                $d_q = MySQL_Query ($d_s);
                while ($d = MySQL_Fetch_Array($d_q)) {
                    if ((date("Y",$d['date'])==$year)&&(date("n",$d['date'])==($i+1))){
                        $pocet++;
                    }
                }
                echo '<td style="text-align: center;border: 1px solid #eee" onmouseover="zvyrazni('.$zam['id'].',1)" onmouseout="zvyrazni('.$zam['id'].',0)">'.$pocet.'</th>';
                $total += $pocet;
            }
            echo '<th style="background: #eee; border: 1px solid #eee" onmouseover="zvyrazni('.$zam['id'].',1)" onmouseout="zvyrazni('.$zam['id'].',0)">';
            echo $total;
            echo '</th>';
            echo '</tr>';
        }
    }
    echo '</table>';
}
?>
